using System;

namespace Gltf
{
    public class Accessor : AccessorSpec
    {
        /*static*/
        public int computeNumberOfComponents(string type)
        {
            if (type == Type.SCALAR)
            {
                return 1;
            }
            if (type == Type.VEC2)
            {
                return 2;
            }
            if (type == Type.VEC3)
            {
                return 3;
            }
            if (type == Type.VEC4)
            {
                return 4;
            }
            if (type == Type.MAT2)
            {
                return 4;
            }
            if (type == Type.MAT3)
            {
                return 9;
            }
            if (type == Type.MAT4)
            {
                return 16;
            }
            // TODO Print a warning here!
            return 0;
        }

        public int computeByteSizeOfComponent(int componentType)
        {
            switch (componentType)
            {
                case ComponentType.BYTE:
                case ComponentType.UNSIGNED_BYTE:
                    return 1;
                case ComponentType.SHORT:
                case ComponentType.UNSIGNED_SHORT:
                    return 2;
                case ComponentType.UNSIGNED_INT:
                case ComponentType.FLOAT:
                    return 4;
                default:
                    // TODO Print a warning here!
                    return 0;
            }
        }

        public int computeNumberOfComponents()
        {
            return computeNumberOfComponents(this.type);
        }

        public int computeByteSizeOfComponent()
        {
            return computeByteSizeOfComponent(this.componentType);
        }

        public Int64 computeBytesPerVertex()
        {
            return (Int64)this.computeByteSizeOfComponent() * (Int64)this.computeNumberOfComponents();
        }

        public Int64 computeByteStride(Model model)
        {
            BufferView pBufferView = Model.getSafe(model.bufferViews, this.bufferView);
            if (!pBufferView)
            {
                return 0;
            }

            if (pBufferView.byteStride.HasValue)
            {
                return pBufferView.byteStride.Value;
            }
            return computeNumberOfComponents(this.type) * computeByteSizeOfComponent(this.componentType);
        }
    }
}